package it.ma.musicplayernew.receiver;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;

import it.ma.musicplayernew.service.CoreService;

/**
 * Author: Victor Wu
 * Email: victor-wu*foxmail.com
 * Time: 2015/11/17 9:46
 * Description: TODO
 */
public class PhoneStateChangeReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {

        Intent play = new Intent(CoreService.ACTION_PLAY);
        Intent pause = new Intent(CoreService.ACTION_PAUSE);

        // 如果是拨打电话
        if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
            context.sendBroadcast(pause);
        } else {
            // 如果是来电
            TelephonyManager tManager = (TelephonyManager) context
                    .getSystemService(Service.TELEPHONY_SERVICE);
            switch (tManager.getCallState()) {
                case TelephonyManager.CALL_STATE_RINGING:
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    context.sendBroadcast(pause);
                    break;
                case TelephonyManager.CALL_STATE_IDLE:
                    context.sendBroadcast(play);
                    break;
            }
        }
    }
}
